package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuSaveInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService {
    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;




    @Transactional
    @Override
    public void saveSpuInfoData(SpuSaveInfoVo vo) {
        //存储基本信息
        SpuInfo spuInfo = new SpuInfo();

        BeanUtils.copyProperties(vo, spuInfo);

        save(spuInfo);

        Long spuInfoId = spuInfo.getId();
        //存储图片


        List<SpuSaveInfoVo.SpuImageListDTO> spuImageList = vo.getSpuImageList();
        List<SpuImage> spuImages = spuImageList.stream().map(item -> {
            SpuImage spuImage = new SpuImage();

            spuImage.setSpuId(spuInfoId);
            BeanUtils.copyProperties(item, spuImage);

            return spuImage;
        }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImages);

        //存储销售属性名 spu_sale_attr
        List<SpuSaveInfoVo.SpuSaleAttrListDTO> spuSaleAttrList = vo.getSpuSaleAttrList();
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrList.stream().map(item -> {
            SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
            spuSaleAttr.setSpuId(spuInfoId);
            BeanUtils.copyProperties(item, spuSaleAttr);
            return spuSaleAttr;
        }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrs);

        //存储销售属性值
        List<SpuSaleAttrValue> saleAttrValues = vo.getSpuSaleAttrList().stream().flatMap(item -> {
            List<SpuSaleAttrValue> spuSaleAttrValues = item.getSpuSaleAttrValueList().stream().map(item1 -> {
                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                spuSaleAttrValue.setSpuId(spuInfoId);
                BeanUtils.copyProperties(item1, spuSaleAttrValue);
                return spuSaleAttrValue;
            }).collect(Collectors.toList());

            //flatMap返回的是流
            return spuSaleAttrValues.stream();
        }).collect(Collectors.toList());

        spuSaleAttrValueService.saveBatch(saleAttrValues);


    }

}




